package com.easysoft.framework.manager.service;

import com.easysoft.commons.utils.UUIDUtils;
import com.easysoft.framework.annotation.jdbc.DataSource;
import com.easysoft.framework.beans.Page;
import com.easysoft.framework.manager.mapper.SysManagerMapperExtd;
import com.easysoft.framework.manager.model.SysManager;
import com.easysoft.framework.manager.model.SysManagerQuery;
import com.easysoft.framework.manager.model.SysManagerView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service("service.SysManagerService")
public class SysManagerService {

    @Autowired
    @Qualifier("mapper.SysManagerMapperExtd")
    private SysManagerMapperExtd sysManagerMapperExtd;

    /**
     * 根据主键删除管理员
     * @param id id
     */
    @DataSource(DataSource.MASTER)
    public int deleteByPrimaryKey(String id) {
        return sysManagerMapperExtd.deleteByPrimaryKey(id);
    }

    /**
     * 添加管理员
     */
    @DataSource(DataSource.MASTER)
    public int addSelective(SysManager sysManager) {
        sysManager.setCreateTime(new Date());
        return sysManagerMapperExtd.insertSelective(sysManager);
    }

    /**
     * 根据主键查询管理员
     * @param id id
     */
    @DataSource(DataSource.SLAVE)
    public SysManager selectByPrimaryKey(String id) {
        return sysManagerMapperExtd.selectByPrimaryKey(id);
    }

    /**
     * 更新管理员信息
     */
    @DataSource(DataSource.MASTER)
    public int updateByPrimaryKeySelective(SysManager sysManager) {
        return sysManagerMapperExtd.updateByPrimaryKeySelective(sysManager);
    }

    /**
     * 更新管理员信息
     */
    @DataSource(DataSource.MASTER)
    public int updateByPrimaryKey(SysManager sysManager) {
        return sysManagerMapperExtd.updateByPrimaryKey(sysManager);
    }

    /**
     * 根据用户名查询管理员
     * @param username 用户名
     */
    @DataSource(DataSource.SLAVE)
    public SysManagerView selectManagerByUserName(String username) {
        return sysManagerMapperExtd.selectManagerByUserName(username);
    }

    /**
     * 统计（用户名或密码）
     */
    @DataSource(DataSource.SLAVE)
    public int countManagerByUserNameOrMobile(String username, String mobile) {
        return sysManagerMapperExtd.countManagerByUserNameOrMobile(username, mobile);
    }

    @DataSource(DataSource.SLAVE)
    public int selectCountAllSysManager(SysManagerQuery query){
        return sysManagerMapperExtd.selectCountAllSysManager(query);
    }

    /**
     * 获取管理员列表
     * @param page 分页参数
     * @param query 查询条件
     * @return 管理员的List集合
     */
    @DataSource(DataSource.SLAVE)
    public List<SysManagerView> selectSysManagerListByPage(Page page, SysManagerQuery query) {
        int maxRow=sysManagerMapperExtd.selectCountAllSysManager(query);
        if (maxRow == 0) {
            return new ArrayList<SysManagerView>();
        }
        page.setMaxRow(maxRow);
        return sysManagerMapperExtd.selectSysManagerListByPage(page, query);
    }

    public SysManagerView selectSysManagerViewByPrimaryKey(String id){
        return sysManagerMapperExtd.selectSysManagerViewByPrimaryKey(id);
    }
}
